package com.asha.vrlib.strategy.display;

import android.graphics.Point;
import android.util.Log;

import com.asha.vrlib.MDVRLibrary;
import com.asha.vrlib.common.MDGLHandler;
import com.asha.vrlib.model.BarrelDistortionConfig;
import com.asha.vrlib.strategy.ModeManager;
import com.asha.vrlib.strategy.projection.AbsProjectionStrategy;
import com.asha.vrlib.strategy.projection.ProjectionModeManager;

import java.util.List;

/**
 * Created by hzqiujiadi on 16/3/19.
 * hzqiujiadi ashqalcn@gmail.com
 */
public class DisplayModeManager extends ModeManager<AbsDisplayStrategy> implements IDisplayMode {

    public static int[] sModes = {MDVRLibrary.DISPLAY_MODE_NORMAL, MDVRLibrary.DISPLAY_MODE_GLASS};

    private boolean antiDistortionEnabled;
    private BarrelDistortionConfig barrelDistortionConfig;

    private int mWidth = -1;
    private int mHeight = -1;

    private Params mParams;

    public DisplayModeManager(int mode, MDGLHandler handler, Params params) {
        super(mode, handler);
        mParams = params;
    }

    @Override
    protected int[] getModes() {
        return sModes;
    }

    @Override
    protected AbsDisplayStrategy createStrategy(int mode) {
        if (mParams != null && mParams.displayFactory != null) {
            AbsDisplayStrategy strategy = mParams.displayFactory.createStrategy(mode, mParams);
            if (strategy != null) return strategy;
        }

        switch (mode){
            case MDVRLibrary.DISPLAY_MODE_GLASS:
                return new GlassStrategy();
            case MDVRLibrary.DISPLAY_MODE_NORMAL:
            default:
                return new NormalStrategy();
        }
    }

    @Override
    public int getVisibleSize() {
        return getStrategy().getVisibleSize();
    }

    public void setAntiDistortionEnabled(boolean antiDistortionEnabled) {
        this.antiDistortionEnabled = antiDistortionEnabled;
    }

    public boolean isAntiDistortionEnabled() {
        return antiDistortionEnabled;
    }

    public void setBarrelDistortionConfig(BarrelDistortionConfig barrelDistortionConfig) {
        this.barrelDistortionConfig = barrelDistortionConfig;
    }

    public BarrelDistortionConfig getBarrelDistortionConfig() {
        return barrelDistortionConfig;
    }


    public void updateStrategyViewportInfo() {
        Log.d("FUYAO_DMManager", "updateStrategyViewportInfo RUN " + getStrategy());
        getStrategy().onSurfaceChanged(mWidth, mHeight);
    }

    @Override
    public void onSurfaceChanged(final int width, final int height) {
        getGLHandler().post(new Runnable() {
            @Override
            public void run() {
                Log.d("FUYAO_DMManager", "onSurfaceChanged RUN " + getStrategy());
                mWidth = width;
                mHeight = height;
                getStrategy().onSurfaceChanged(width, height);
            }
        });
    }

    @Override
    public List<Point> getDisplayPoss() {
        return getStrategy().getDisplayPoss();
    }

    @Override
    public List<Point> getDisplaySizes() {
        return getStrategy().getDisplaySizes();
    }

    public static class Params {
        public ProjectionModeManager projectionModeManager;
        public IFYDisplayFactory displayFactory;
    }
}
